package com.qiange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qiange.common.BaseContext;
import com.qiange.common.Result;
import com.qiange.pojo.AddressBook;
import com.qiange.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        Long userId = BaseContext.getId();

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        lqw.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> addressBookList = addressBookService.list(lqw);

        return Result.success(addressBookList);
    }

    @PostMapping
    public Result<String> addAddress(@RequestBody AddressBook addressBook) {
        Long userId = BaseContext.getId();

        addressBook.setUserId(userId);

        addressBookService.save(addressBook);

        return Result.success("增加地址成功");
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result<String> setDefaultAddr(@RequestBody AddressBook addressBook) {

        Long userId = BaseContext.getId();
        //先将该用户的默认地址字段全部设置成不是默认地址 0

        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        lqw.set(AddressBook::getIsDefault, 0);

        addressBookService.update(lqw);

        //再将该id所对应的地址的默认地址字段设置为 1 ，显示为默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return Result.success("设置默认地址成功");

    }

    @GetMapping("/{id}")
    public Result<AddressBook> selectById(@PathVariable Long id) {


        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook != null) {
            return Result.success(addressBook);
        } else {
            return Result.error("没有找到该地址");
        }
    }

    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);

        return Result.success("修改地址信息成功");

    }

    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);

        return Result.success("删除成功");
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return Result.error("没有找到该对象");
        } else {
            return Result.success(addressBook);
        }
    }

}
